package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Food;
import com.itheima.pojo.Sports;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author houzongtao
 * @date 2020/9/1 14:53
 */
@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodDao foodDao;

    //删除
    public void delete(Integer id){
        foodDao.deleteById(id);
    }

    //编辑
    public void edit(Food food) {
        foodDao.edit(food);
    }

    public Food findById(Integer id) {
        return foodDao.findById(id);
    }

    //分页查询
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Food> page = foodDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Food food) {
        foodDao.add(food);
    }

    //批量导入
    public void batchAdd(List<Food> foodsList) {
        if(foodsList != null && foodsList.size() > 0){
            for (Food food : foodsList) {
                //检查此数据（Id）是否存在
                long count = foodDao.findCountById(food.getId());
                if(count > 0){
                    //已经存在，执行更新操作
                    foodDao.editFoodById(food);
                }else{
                    //不存在，执行添加操作
                    foodDao.add(food);
                }
            }
        }
    }
}
